<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ExistMore2More
 *
 * @author Moon
 */
class ExistMore2More extends CExistValidator {
//put your code here
    public $joinTable ='';

    public $joinTableAttribute = '';

    protected function validateAttribute($object,$attribute) {
        $value=$object->$attribute;
        if($this->allowEmpty && $this->isEmpty($value))
            return;
        if(!empty($this->joinTable) && !empty($this->joinTableAttribute)) {
            $db = $object->dbConnection;
            $table = $db->commandBuilder->getSchema()->getTable($this->joinTable);
            if(($column=$table->getColumn($this->joinTableAttribute))===null)
                throw new CException(Yii::t('yii','Column "{column}" does not exist in table "{table}".',
                array('{column}'=>$this->joinTableAttribute,'{table}'=>$table->name)));
            $criteria = new CDbCriteria;
            $criteria->condition = $this->joinTableAttribute.' = :cargo_id';
            $criteria->params = array(':cargo_id'=>$value);
            if($this->criteria!==array()) {
                $criteria=new CDbCriteria($criteria);
                $criteria->mergeWith($this->criteria);
            }
            if($db->commandBuilder->createFindCommand($this->joinTable,$criteria)->execute()<=0) {
                $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} "{value}" is invalid.');
                $this->addError($object,$attribute,$message,array('{value}'=>$value));
            }
        }
    }
}
?>
